/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Component;

import GameObject.Sprite;
import Global.ResourceManager;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
 *
 * @author Huy
 */
public class MyComboBox extends MyControlItem {

	private ArrayList<String> items;
	private String text;
	private ArrayList<MyTextButton> buttons;
	private boolean isExplained;
	private int selectedIndex;

	public MyComboBox(BufferedImage textImg, BufferedImage img, String[] items, int x, int y) {
		init(textImg, img, items, x, y);
	}

	public MyComboBox(String[] items, int x, int y) {
		init(ResourceManager.getImage("default/textbox"),ResourceManager.getImage("default/combobox"), items, x, y);
	}

	private void init(BufferedImage textImg, BufferedImage img, String[] items, int x, int y) {
		this.items = new ArrayList<String>();
		this.buttons = new ArrayList<MyTextButton>();

		for (int i = 0; i < items.length; i++) {
			this.items.add(items[i]);
			MyTextButton button = new MyTextButton(img, items[i], x, y + textImg.getHeight() / 2 + i * (img.getHeight() / 3 - 3));
			button.setVisible(false);
			this.buttons.add(button);
		}
		if (items.length > 0) {
			text = items[0];
		}
		selectedIndex = 0;
		isExplained = false;
		sprite = new Sprite(textImg, 1, 2, x, y);
	//	sprite.setScalePosition(true);
	}

	public int getSelectedIndex() {
		return selectedIndex;
	}

	public void setSelectedIndex(int i) {
		selectedIndex = i;
		text = items.get(i);
	}

	public String getText() {
		return text;
	}

	@Override
	public void move(int dx, int dy) {
		sprite.move(dx, dy);
		for (MyTextButton button : buttons) {
			button.move(dx, dy);
		}
	}

	@Override
	public void draw(Graphics2D g) {
		if (!isVisible) {
			return;
		}
		sprite.draw(g);
		if (isEnable) {
			g.setColor(foreColor);
		} else {
			g.setColor(disableColor);
		}
		Rectangle2D textBound = g.getFont().getStringBounds(text, g.getFontRenderContext());
		Rectangle spriteBound = sprite.getBound();
		int x = (int) (spriteBound.getCenterX() - textBound.getCenterX());
		int y = (int) (spriteBound.getCenterY() - textBound.getCenterY());
		g.drawString(text, x, y);

		for (MyTextButton button : buttons) {
			button.draw(g);
		}

	}

	@Override
	public void setScaleX(float s) {
		sprite.setScaleX(s);
		for (MyTextButton button : buttons) {
			button.setScaleX(s);
		}
	}
	@Override
	public void setScaleY(float s) {
		sprite.setScaleY(s);
		for (MyTextButton button : buttons) {
			button.setScaleY(s);
		}
	}
	@Override
	public boolean mouseMoved(MouseEvent e) {
		if (e == null || !isEnable || !isVisible) {
			((Sprite)sprite).setFrame(0);
			return false;
		}
		boolean flag = false;
		if (super.mousePressed(e)) {
			((Sprite)sprite).setFrame(1);
			return true;
		}
		((Sprite)sprite).setFrame(0);
		if (isExplained) {
			for (MyTextButton button : buttons) {
				if (flag) {
					button.mouseMoved(null);
				} else if (button.mouseMoved(e)) {
					flag = true;
				}
			}
		}
		return flag;
	}

	@Override
	public boolean mousePressed(MouseEvent e) {
		if (!isEnable || !isVisible) {
			return false;
		}
		boolean flag = false;
		if (!super.mousePressed(e) && isExplained) {
			for (MyTextButton button : buttons) {
				if (button.mousePressed(e)) {
					return true;
				}
			}
		}

		return flag;
	}

	@Override
	public boolean mouseReleased(MouseEvent e) {
		if (!isEnable || !isVisible) {
			return false;
		}
		if (super.mouseReleased(e)) {
			setExplained(!isExplained);
			return true;
		}
		boolean flag = false;
		if (isExplained) {
			int i = 0;
			for (MyTextButton button : buttons) {

				if (button.mouseReleased(e)) {
					text = button.getText();
					selectedIndex = i;
					setExplained(false);
					if (mouseHandler != null) {
						mouseHandler.mouseReleased(e);
					}
					return true;
				}
				i++;
			}
		}

		setExplained(false);
		return flag;
	}

	private void setExplained(boolean isEpn) {
		isExplained = isEpn;
		for (MyTextButton button : buttons) {
			button.setVisible(isEpn);
		}
	}
}
